#include	"Wiimote4Virtools.h"
#include	"WiimLEDs.h"

// http://digitalretrograde.com/projects/wiim/

CKObjectDeclaration *FillBehaviorWiimLEDs()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration ( "WiimLEDs" );
	od->SetDescription ( "Sets states of a Wiimote's LEDs." );
	od->SetCategory ( "Wiimote" );
	od->SetType ( CKDLL_BEHAVIORPROTOTYPE );
	od->SetGuid ( WIIMLEDS_GUID );
	od->SetAuthorGuid ( AUTHOR_GUID );
	od->SetAuthorName ( "Jonathan DERROUGH" );
	od->SetVersion ( 0x00000001 );
	od->SetCreationFunction ( CreateWiimLEDsProto );
	//od->SetCompatibleClassId ( CKCID_3DENTITY );
	return od;
}

CKERROR CreateWiimLEDsProto ( CKBehaviorPrototype **pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("WiimLEDs");

	if(!proto)
		return (CKERR_OUTOFMEMORY);

	proto->DeclareInput("In");
	proto->DeclareOutput("Out");

	proto->DeclareInParameter("Wiimote id", CKPGUID_INT );
	proto->DeclareInParameter("LED 1", CKPGUID_BOOL );
	proto->DeclareInParameter("LED 2", CKPGUID_BOOL );
	proto->DeclareInParameter("LED 3", CKPGUID_BOOL );
	proto->DeclareInParameter("LED 4", CKPGUID_BOOL );

	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(WiimLEDs);
	//proto->SetBehaviorFlags(CKBEHAVIOR_TARGETABLE);

	*pproto = proto;
	return (CK_OK);
}

int WiimLEDs(const CKBehaviorContext& behcontext)
{
	CKBehavior *beh = behcontext.Behavior;

	beh->ActivateInput(0, FALSE);

	// Wiimote(s) initialized ?
	if (g_wiimotes.size() < 1)
	{
		beh->ActivateOutput(0);
		return (CKBR_GENERICERROR);
	}

	// Get the id of the Wiimote we want to rumble
	int wiimote_id = 0;
	beh->GetInputParameterValue(0, &wiimote_id);

	// Strength of rumble ?
	CKBOOL led1 = 0;
	CKBOOL led2 = 0;
	CKBOOL led3 = 0;
	CKBOOL led4 = 0;
	
	beh->GetInputParameterValue(1, &led1);
	beh->GetInputParameterValue(2, &led2);
	beh->GetInputParameterValue(3, &led3);
	beh->GetInputParameterValue(4, &led4);
	
	
	g_wiimotes[wiimote_id].SetLEDs(led1 == FALSE ? false : true,
																										 led2 == FALSE ? false : true, 
																										 led3 == FALSE ? false : true, 
																										 led4 == FALSE ? false : true);

	beh->ActivateOutput(0);
	return (CK_OK);
}